﻿$G.pay.ui = {
	_hp: "",
	a: null, lblVendorName: function () { if (!this.a) this.a = $("#lblVendorName"); return this.a; },
	b: null, lblConfirmVendorName: function () { if (!this.b) this.b = $("#lblConfirmVendorName"); return this.b; },
	c: null, lblCashierName: function () { if (!this.c) this.c = $("#lblCashierName"); return this.c; },
	d: null, lblCustomerName: function () { if (!this.d) this.d = $("#lblCustomerName"); return this.d; },
	e: null, lblConfirmCustomerName: function () { if (!this.e) this.e = $("#lblConfirmCustomerName"); return this.e; },
	f: null, lblCredit: function () { if (!this.f) this.f = $("#lblCredit"); return this.f; },
	g: null, tbPin: function () { if (!this.g) this.g = $(".tbPin"); return this.g; },
	h: null, num_key: function () { if (!this.h) this.h = $(".num-key"); return this.h; },
	i: null, btnApprove: function () { if (!this.i) this.i = $("#btnApprove"); return this.i; },
	j: null, tbAmount: function () { if (!this.j) this.j = $(".amount"); return this.j; },
	k: null, btnCheck: function () { if (!this.k) this.k = $("#btnCheck"); return this.k; },
	l: null, ddlPosNeg: function () { if (!this.l) this.l = $("#ddlPosNeg"); return this.l; },
	m: null, tbEuros: function () { if (!this.m) this.m = $(this.tbAmount()[0]); return this.m; },
	n: null, tbCents: function () { if (!this.n) this.n = $(this.tbAmount()[1]); return this.n; },
	o: null, lblConfirmAmount: function () { if (!this.o) this.o = $("#lblConfirmAmount"); return this.o; },
	p: null, lblConfirmActionText: function () { if (!this.p) this.p = $("#lblConfirmActionText"); return this.p; },
	q: null, btnVendorLogin: function () { if (!this.q) this.q = $("#btnVendorLogin"); return this.q; },
	r: null, btnChgAccount: function () { if (!this.r) this.r = $("#btnChgAccount"); return this.r; },
	s: null, btnDisApprove: function () { if (!this.s) this.s = $("#btnDisApprove"); return this.s; },
	t: null, tbVendorWW: function () { if (!this.t) this.t = $("#tbVendorWW"); return this.t; },
	u: null, tbVendorUserName: function () { if (!this.u) this.u = $("#tbVendorUserName"); return this.u; },
	v: null, cbEmail: function () { if (!this.v) this.v = $("#cbEmail"); return this.v; },
	w: null, lblGotMail: function () { if (!this.w) this.w = $("#lblGotMail"); return this.w; },
	x: null, lblErrorMessage: function () { if (!this.x) this.x = $("#lblErrorMessage"); return this.x; },
	
	initialize: function () {

		this.btnCheck().click(this.btnCheckClick);

		this.btnApprove().click(this.btnApproveClick);

		this.btnVendorLogin().click(this.btnVendorLoginClick);

		this.btnChgAccount().click(this.btnChgAccountClick);

		this.btnDisApprove().click(this.btnDisApproveClick);

		this.ddlPosNeg().change(this.paybackToggle.bind(this));

		this.clearPin();

		this.num_key().on("touchstart click", function (e) {
			e.stopPropagation(); e.preventDefault();
			$G.pay.ui.numKeyTouch.call($G.pay.ui, $(this).val());
		});
	},

	btnCheckClick: function () {
		$G.pay.prePayCheck();
	},

	btnApproveClick: function () {
		$G.pay.approve();
	},

	btnVendorLoginClick: function () {
		$G.pay.vendorLogin();
	},

	btnChgAccountClick: function () {
		$G.changePage("#pnlVendorLogin");
	},

	btnDisApproveClick: function () {
		$G.pay.ui.confirm($G.pay.texts.confirmDisapprove, function () { $G.changePage("#pnlDisapproved"); });
	},

	numKeyTouch: function (key) {
		var l = this._hp.length;
		switch (key) { case 'D': this._hp = this._hp.slice(0, -1); break; case 'C': this._hp = ""; break; default: if (key !== ' ' && l < 4) this._hp += key; break; }

		l = this._hp.length;

		this.tbPin().slice(0, l).text('✱');
		this.tbPin().slice(l).text('●');

		this.btnApprove().toggle(l === 4);
	},

	clearPin: function () {
		this.numKeyTouch('C');
	},

	disableCheck: function() {
		this.tbAmount().prop('disabled', true);
		this.btnCheck().hide();
	},

	isPayBack: function () {
		return (this.ddlPosNeg().val() == '2');
	},

	paybackToggle: function () {
		var pb = this.isPayBack();
		this.tbAmount().css('background-color', pb ? '#ff8080' : 'white');
		this.lblConfirmActionText().text(pb ? $G.pay.texts.toReceive : $G.pay.texts.toPay);
	},

	warn: function (msg) {
		$.prompt(msg, { title: 'Let op' });
	},

	info: function (msg) {
		$.prompt(msg, { title: 'Info' });
	},

	error: function (msg) {
		$.prompt(msg, { title: 'Fout' });
	},

	confirm: function (msg, yesCallback, noCallback) {
		$.prompt(msg,
			{
				title: 'Bevestigen',
				buttons: { 'Ja': true, 'Nee': false },
				submit: function (e, v, m, f) {
					if (v) if (typeof yesCallback !== undefined) yesCallback();
					else if (typeof noCallback !== undefined) noCallback();
				}
			});
	},

	fatal: function (msg) {
		this.lblErrorMessage().text(msg);
		$G.changePage("#pnlErrorNoRetry");
	},

	setPaymentInfo: function (d) {
		this.lblVendorName().text(d.VendorName);
		this.lblConfirmVendorName().text(d.VendorName);
		this.lblCashierName().text(d.CashierFullName);
		this.lblCustomerName().text(d.CustomerFullName);
		this.lblConfirmCustomerName().text(d.CustomerFullName);
		this.lblCredit().text(d.CreditString);
		if (d.AllowRefund == false) {
			this.ddlPosNeg().val('1');
			$('#pnlDdlPosNeg').hide();
			$('#lblNoRefundLabel').show();
		}
		else {
			$('#pnlDdlPosNeg').show();
			$('#lblNoRefundLabel').hide();
		}
	},

	loader: function (show) {
		if (show) {
			$.mobile.loading('show', {
				text: $G.pay.texts.ajaxLoadingText,
				textVisible: true,
				theme: 'a',
				textonly: false,
				html: ''
			});
		}
		else $.mobile.loading('hide');
	}
}
